/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package digital_ink;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Hyperlink;
import com.sun.webui.jsf.component.ImageComponent;
import com.sun.webui.jsf.component.ImageHyperlink;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.PasswordField;
import com.sun.webui.jsf.component.StaticText;
import com.sun.webui.jsf.component.TextField;
import java.io.Reader;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;

/**
 * <p>Fragment bean that corresponds to a similarly named JSP page
 * fragment.  This class contains component definitions (and initialization
 * code) for all components that you have defined on this fragment, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version inicioSesion.java
 * @version Created on 13-dic-2008, 15:32:42
 * @author Administrador
 */

public class inicioSesion extends AbstractFragmentBean {


    public static String nombre;


    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization. <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private Button buttonIniciar = new Button();

    public Button getButtonIniciar() {
        return buttonIniciar;
    }

    public void setButtonIniciar(Button b) {
        this.buttonIniciar = b;
    }
    private TextField textFieldUsuario = new TextField();

    public TextField getTextFieldUsuario() {
        return textFieldUsuario;
    }

    public void setTextFieldUsuario(TextField tf) {
        this.textFieldUsuario = tf;
    }
    private StaticText staticTextError = new StaticText();

    public StaticText getStaticTextError() {
        return staticTextError;
    }

    public void setStaticTextError(StaticText st) {
        this.staticTextError = st;
    }
    private Label labelUsuario = new Label();

    public Label getLabelUsuario() {
        return labelUsuario;
    }

    public void setLabelUsuario(Label l) {
        this.labelUsuario = l;
    }
    private Label labelContraseña = new Label();

    public Label getLabelContraseña() {
        return labelContraseña;
    }

    public void setLabelContraseña(Label l) {
        this.labelContraseña = l;
    }
    private StaticText staticText2 = new StaticText();

    public StaticText getStaticText2() {
        return staticText2;
    }

    public void setStaticText2(StaticText st) {
        this.staticText2 = st;
    }
    private Hyperlink hyperlink1 = new Hyperlink();

    public Hyperlink getHyperlink1() {
        return hyperlink1;
    }

    public void setHyperlink1(Hyperlink h) {
        this.hyperlink1 = h;
    }
    private StaticText staticText3 = new StaticText();

    public StaticText getStaticText3() {
        return staticText3;
    }

    public void setStaticText3(StaticText st) {
        this.staticText3 = st;
    }
    private StaticText staticText1 = new StaticText();

    public StaticText getStaticText1() {
        return staticText1;
    }

    public void setStaticText1(StaticText st) {
        this.staticText1 = st;
    }
    private ImageComponent image2 = new ImageComponent();

    public ImageComponent getImage2() {
        return image2;
    }

    public void setImage2(ImageComponent ic) {
        this.image2 = ic;
    }
    private PasswordField passwordField1 = new PasswordField();

    public PasswordField getPasswordField1() {
        return passwordField1;
    }

    public void setPasswordField1(PasswordField pf) {
        this.passwordField1 = pf;
    }
    private Button button1 = new Button();

    public Button getButton1() {
        return button1;
    }

    public void setButton1(Button b) {
        this.button1 = b;
    }
    private ImageHyperlink imageHyperlink1 = new ImageHyperlink();

    public ImageHyperlink getImageHyperlink1() {
        return imageHyperlink1;
    }

    public void setImageHyperlink1(ImageHyperlink ih) {
        this.imageHyperlink1 = ih;
    }
    private ImageHyperlink imageHyperlink2 = new ImageHyperlink();

    public ImageHyperlink getImageHyperlink2() {
        return imageHyperlink2;
    }

    public void setImageHyperlink2(ImageHyperlink ih) {
        this.imageHyperlink2 = ih;
    }
    private ImageHyperlink imageHyperlink3 = new ImageHyperlink();

    public ImageHyperlink getImageHyperlink3() {
        return imageHyperlink3;
    }

    public void setImageHyperlink3(ImageHyperlink ih) {
        this.imageHyperlink3 = ih;
    }
    private ImageHyperlink imageHyperlink4 = new ImageHyperlink();

    public ImageHyperlink getImageHyperlink4() {
        return imageHyperlink4;
    }

    public void setImageHyperlink4(ImageHyperlink ih) {
        this.imageHyperlink4 = ih;
    }
    private StaticText staticText4 = new StaticText();

    public StaticText getStaticText4() {
        return staticText4;
    }

    public void setStaticText4(StaticText st) {
        this.staticText4 = st;
    }
    // </editor-fold>
    public inicioSesion() {
    }

    /**
     * <p>Callback method that is called whenever a page containing
     * this page fragment is navigated to, either directly via a URL,
     * or indirectly via page navigation.  Override this method to acquire
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        
        // <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called.  Override this
     * method to release resources acquired in the <code>init()</code>
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String buttonIniciar_action() {
       // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same pag
        /*if (textFieldUsuario.getText().equals("ale")){
            System.out.println("OK");
            return "ok";
        }
        System.out.println("fallo");
        this.getSessionBean1().setUsuario("aki ta la cosa esa");*/
        Reader reader = null;
        try{
             reader = Resources.getResourceAsReader("ibatis-config.xml");

             SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

             Persona p = new Persona();
             p.setSeudonimo((String) textFieldUsuario.getText());
             p.setPassword((String) passwordField1.getText());
             this.getSessionBean1().setUsuarioNombre((String) textFieldUsuario.getText());
             
             Persona a = (Persona) sqlMap.queryForObject("buscarPersonaPorNombre", p);
             if(a.getPassword().equals(passwordField1.getText())){
                 
             textFieldUsuario.setVisible(false);
             buttonIniciar.setVisible(false);
             labelUsuario.setVisible(false);
             labelContraseña.setVisible(false);
             staticText2.setVisible(false);
             hyperlink1.setVisible(false);
             staticText3.setVisible(true);
             //hyperlink2.setVisible(true);
             //staticText6.setVisible(true);
             staticText1.setVisible(false);
             image2.setVisible(true);
             staticText4.setVisible(true);
             staticText4.setText(p.getSeudonimo());
             passwordField1.setVisible(false);
             button1.setVisible(true);
             imageHyperlink1.setVisible(true);
             imageHyperlink2.setVisible(true);
             imageHyperlink3.setVisible(true);
             imageHyperlink4.setVisible(true);
             //this.getSessionBean1().setUsuario("1");
             }else{
                 staticTextError.setText("Error password");
                 staticTextError.setVisible(true);
               //  this.getSessionBean1().setUsuario("0");
             }
        }catch(Exception ex){
            System.out.println(ex);
            this.getSessionBean1().setUsuario("0");
        }


        return "Registro";
    }

    public void textFieldUsuario_processValueChange(ValueChangeEvent vce) {
    }

    public String hyperlink1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "case1";
    }

    public String button1_action() {
               //staticTextError.setVisible(true);
             textFieldUsuario.setVisible(true);
             buttonIniciar.setVisible(true);
             labelUsuario.setVisible(true);
             labelContraseña.setVisible(true);
             staticText2.setVisible(true);
             hyperlink1.setVisible(true);
             staticText3.setVisible(false);
             staticText1.setVisible(true);
             image2.setVisible(false);
             staticText4.setVisible(false);
             
             passwordField1.setVisible(true);
             button1.setVisible(false);
             //this.getSessionBean1().setUsuario("0");
        return null;
    }

    public String hyperlink2_action() {
        Reader reader = null;
        try{
             reader = Resources.getResourceAsReader("ibatis-config.xml");

             SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

             Persona p = new Persona();
             p.setSeudonimo(nombre);


             Persona a = (Persona) sqlMap.queryForObject("buscarPersonaPorNombre", p);

        }catch(Exception ex){
            System.out.println(ex);
        }
        return null;
    }

   

    

}
